package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.vecmath.Vector3f;

import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.StaticLightPropertyFrame;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.object.light.DirectionalLight;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.DirectionalLightAppearance3D;

public class DirectionalLightPropertyFrameFactory implements PropertyFrameFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return DirectionalLight.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		
		return new PropertyFrameImpl(model, (DirectionalLight) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends StaticLightPropertyFrame {

		// The input fields for this property frame
		private final JTextField	xInput, yInput, zInput;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(EditorModel model, DirectionalLight instance) {
			super(model, instance);
			
			DirectionalLightAppearance3D appearance3d = 
					(DirectionalLightAppearance3D) instance.getAppearance();
			
			JPanel panel = new JPanel();
			panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
			
			JPanel directionPanel = new JPanel();
			directionPanel.setBorder(BorderFactory.createTitledBorder("Direction"));
			directionPanel.setLayout(new GridLayout(2,3));
			directionPanel.add(new JLabel("x:"));
			directionPanel.add(new JLabel("y:"));
			directionPanel.add(new JLabel("z:"));
			Vector3f direction = new Vector3f();
			appearance3d.getDirection(direction);
			xInput = new JTextField("" + direction.getX());
			yInput = new JTextField("" + direction.getY());
			zInput = new JTextField("" + direction.getZ());
			directionPanel.add(xInput);
			directionPanel.add(yInput);
			directionPanel.add(zInput);
			panel.add(directionPanel);
			
			addPanel("Direction",panel);
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/
		
		@Override public boolean apply(Collection<String> errorMessages) {
			if (super.apply(errorMessages)) try {
				WorldStaticObject instance = (WorldStaticObject) getInstance();
				DirectionalLightAppearance3D appearance3d = 
						(DirectionalLightAppearance3D) instance.getAppearance();
				final float x = Float.parseFloat(xInput.getText());
				final float y = Float.parseFloat(yInput.getText());
				final float z = Float.parseFloat(zInput.getText());
				Vector3f direction = new Vector3f(x,y,z);
				appearance3d.setDirection(direction);
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			} else
				return false;
		}
		
	}

}
